var app = getApp();
Page({
  data: {
    //商品详情
    goodsInfo: null,
    //已选规格
    spec: null,
    //打开方式
    openType: 1,
    //打开途径
    selType: 1,
    //是否隐藏
    specHidden: true
  },

  onLoad: function(options) {
    var shoppingCarList = app.globalData.shoppingCarList;
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15,
      shoppingCarList: shoppingCarList
    })
    this.getGoodsInfo(options.goodsId);
  },
  //滚动
  onPageScroll:function(){
    if (!this.data.specHidden){
      return;
    }
  },
  //获取商品信息
  getGoodsInfo: function(id) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getGoodsInfo',
      data: {
        goodsId: id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var goodsInfo = r.data.data;
          var min = goodsInfo.specList[0].price;
          var max = goodsInfo.specList[0].price;
          for (var i of goodsInfo.specList) {
            if (i.price < min) {
              min = i.price;
            }
            if (i.price > max) {
              max = i.price;
            }
          }
          goodsInfo.priceAmong = min + '-' + max;
          goodsInfo.specList[0].quantity = 1;
          that.setData({
            goodsInfo: goodsInfo,
            spec: goodsInfo.specList[0]
          })
        } else {
          wx.showToast({
            title: '获取商品信息失败',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取商品信息失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //返回商品列表
  rgoods: function() {
    var pages = getCurrentPages();
    var page = pages[pages.length - 2];
    page.setData({
      pageIndex: 2
    })
    wx.navigateBack({
      delta: 1
    })
  },
  //查看购物车
  rshoppingcar: function() {
    wx.navigateTo({
      url: '/pages/shoppingCar/shoppingCar',
    })
  },
  //关闭属性框
  closeSpec: function() {
    this.setData({
      specHidden: true
    })
  },
  //打开属性框
  openSpec: function(e) {
    var type = e.currentTarget.dataset.type;
    if (type == 'd') {
      this.setData({
        openType: 1
      })
    } else {
      if (type == 's') {
        this.setData({
          openType: 2,
          selType: 1
        })
      } else {
        this.setData({
          openType: 2,
          selType: 2
        })
      }
    }
    this.setData({
      specHidden: false
    })
  },
  //切换规格
  selSpec: function(e) {
    var index = e.currentTarget.dataset.index;
    var spec = this.data.goodsInfo.specList[index];
    spec.quantity = 1;
    this.setData({
      spec: spec
    })
  },
  //增加获减少
  mathQuantity: function(e) {
    var type = e.currentTarget.dataset.type;
    var spec = this.data.spec;
    if (type == 'jian') {
      if (spec.quantity == 1) {
        return;
      } else {
        spec.quantity = spec.quantity - 1;
      }
    } else {
      spec.quantity = spec.quantity + 1;
    }
    this.setData({
      spec: spec
    })
  },
  //添加或更改
  updateOrAddShoppingCar: function (e) {
    var specId = this.data.spec.specId;
    var goodsId = this.data.spec.goodsId;
    var shoppingCar = app.globalData.shoppingCarList;
    for (var i of shoppingCar) {
      if (i.specId == specId) {
        var quantity = parseInt(i.quantity) + this.data.spec.quantity;
        this.updateGoodsQuantity(goodsId, quantity, specId);
        return;
      }
    }
    this.addShoppingCar(goodsId, specId);
  },

  //添加商品到购物车
  addShoppingCar: function (id, specId) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'addGoods',
      data: {
        userId: app.globalData.user.id,
        goodsId: id,
        quantity: 1,
        spec: specId
      },
      method: 'POST',
      success: function (r) {
        if (r.data.code == 200) {
          app.globalData.shoppingCarList = r.data.data;
          that.setData({
            shoppingCarList: r.data.data,
            specHidden:true
          })
          wx.showToast({
            title: '商品已成功添加到购物车',
            icon:'none',
            duration:2000
          })
        } else {
          wx.showToast({
            title: '加入购物车失败',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络断开连接,加入购物车失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  //更改购物车商品数量
  updateGoodsQuantity: function (id, quantity, specId) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'updateGoodsQuantity',
      data: {
        userId: app.globalData.user.id,
        goodsId: id,
        quantity: quantity,
        spec: specId
      },
      method: 'POST',
      success: function (r) {
        if (r.data.code == 200) {
          app.globalData.shoppingCarList = r.data.data;
          that.setData({
            shoppingCarList: r.data.data,
            specHidden: true
          })
          wx.showToast({
            title: '商品已成功添加到购物车',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '加入购物车失败',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络断开连接,加入购物车失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // addShoppingCar: function() {
  //   var that =this;
  //   wx.request({
  //     url: app.globalData.requestUrl + 'addGoods',
  //     data: {
  //       userId: app.globalData.user.id,
  //       goodsId: that.data.spec.goodsId,
  //       quantity: that.data.spec.quantity,
  //       spec: that.data.spec.specId
  //     },
  //     method: 'POST',
  //     success: function(r) {
  //       if (r.data.code == 200) {
  //         // app.globalData.shoppingCarList = r.data.data;
  //         // that.setData({
  //         //   shoppingCarQuantity: r.data.data.length
  //         // })
  //       } else {
  //         wx.showToast({
  //           title: '加入购物车失败',
  //           icon: 'none',
  //           duration: 2000
  //         })
  //       }
  //     },
  //     fail: function() {
  //       wx.showToast({
  //         title: '网络断开连接,加入购物车失败',
  //         icon: 'none',
  //         duration: 2000
  //       })
  //     }
  //   })
  // }
})